package org.ysu.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.ysu.mapper.CCadreMapper;
import org.ysu.mapper.VCadreMapper;
import org.ysu.mapper.VerifierMapper;
import org.ysu.pojo.CCadre;
import org.ysu.pojo.User;
import org.ysu.pojo.VCadre;
import org.ysu.pojo.Verifier;
import org.ysu.service.UserService;
import org.ysu.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.PrivateKey;
import java.util.List;

/**
* @author libaoyu
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-06-17 13:10:27
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    /***
     * jdk提供
     */
    @Resource
    private UserMapper userMapper;
    @Resource
    private VCadreMapper vCadreMapper;
    @Resource
    private VerifierMapper verifierMapper;
    @Resource
    private CCadreMapper cCadreMapper;

    @Override
    public List<User> getUsers(String name) {
        /***
         * 条件查询
         * 使用mybatisplus的条件构造器完成条件查询
         * QueryWrapper
         * 和数据库中的列名一一对应
         */
        /*
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",1)
                .like("name",name)
                .or()
                .ge("age",20);
         */
        /*
        和类中的属性名对应
         */
        LambdaQueryWrapper<User>wrapper=new LambdaQueryWrapper<>();
        //wrapper.like(User::getName,name);
        wrapper.like(StringUtils.isNotBlank(name),User::getName,name);
        List<User> users=userMapper.selectList(wrapper);
        return users;
    }

    /***
     * 判断用户身份
     * @param username
     * @param password
     * @return
     */
    @Override
    public User checkUser(String username, String password) {
        User u=new User();
        //判断是否为乡村干部
        VCadre vCadre = vCadreMapper.selectOne(new QueryWrapper<VCadre>().eq("phone", username));
        if(vCadre!=null && vCadre.getPassword().equals(password)){
            u.setId(vCadre.getVcId());
            u.setName(vCadre.getName());
            u.setAvator(vCadre.getAvator());
            u.setPhone(vCadre.getPhone());
            u.setIdnumber(vCadre.getIdnumber());
            u.setRId(vCadre.getRId());
            u.setPassword(vCadre.getPassword());
            u.setType(1);
            return u;
        }
        //判断是否为核实员
        Verifier ve=verifierMapper.selectOne(new QueryWrapper<Verifier>().eq("phone",username));
        if(ve!=null && ve.getPassword().equals(password)){
            u.setId(ve.getVId());
            u.setName(ve.getName());
            u.setAvator(ve.getAvator());
            u.setPhone(ve.getPhone());
            u.setIdnumber(ve.getIdnumber());
            u.setRId(ve.getRId());
            u.setPassword(ve.getPassword());
            u.setType(3);
            return u;
        }
        //判断是否为省市县干部
        CCadre cc=cCadreMapper.selectOne(new QueryWrapper<CCadre>().eq("phone",username));
        if(cc!=null && cc.getPassword().equals(password)){
            u.setId(cc.getCcId());
            u.setName(cc.getName());
            u.setAvator(cc.getAvator());
            u.setPhone(cc.getPhone());
            u.setIdnumber(cc.getIdnumber());
            u.setRId(cc.getRId());
            u.setPassword(cc.getPassword());
            u.setType(2);
            return u;
        }
        //未找到用户，id置为-1
        u.setId(-1);
        return u;
    }
}




